package com.jjwxc.jwjskandriod.activity;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.adapter.AdolescentBookAdapter;
import com.jjwxc.jwjskandriod.config.BaseActivity;

//青少年主界面
public class AdolescentBookActivity extends BaseActivity {
    /**
     * 菜单标题
     */
    private final String[] TAB_TITLES = new String[]{"书架", "推荐", "我的"};
    /**
     * 菜单图标
     */
    private final int[] TAB_IMGS = new int[]{R.drawable.bookshelf_main_tab, R.drawable.suggest_main_tab,  R.drawable.my_main_tab};
    ViewPager viewPager;
    TabLayout tabLayout;
    /**
     * 页卡适配器
     */
    private PagerAdapter adapter;

    @Override
    public int getContentViewId() {
        return R.layout.activity_adolescentbook;
    }

    @Override
    public void findView() {
        setNoTitle();
        viewPager = findViewById(R.id.view_pager);
        tabLayout = findViewById(R.id.tab_layout);
    }

    @Override
    public void setListener() {

    }

    @Override
    public void afterCreate() {
        // 初始化页卡
        initPager();
        setTabs(tabLayout, getLayoutInflater(), TAB_TITLES, TAB_IMGS);
    }

    /**
     * 设置页卡显示效果
     *
     * @param tabLayout
     * @param inflater
     * @param tabTitlees
     * @param tabImgs
     */
    private void setTabs(TabLayout tabLayout, LayoutInflater inflater, String[] tabTitlees, int[] tabImgs) {
        for (int i = 0; i < tabImgs.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            View view = inflater.inflate(R.layout.item_main_menu, null);
            // 使用自定义视图，目的是为了便于修改，也可使用自带的视图
            tab.setCustomView(view);
            TextView tvTitle = (TextView) view.findViewById(R.id.txt_tab);
            tvTitle.setText(tabTitlees[i]);
            ImageView imgTab = (ImageView) view.findViewById(R.id.img_tab);
            imgTab.setImageResource(tabImgs[i]);
            tabLayout.addTab(tab);
        }
        viewPager.setCurrentItem(1);
        tabLayout.getTabAt(1).select();
    }

    private void initPager() {
        adapter = new AdolescentBookAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);

        // 关联切换
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 取消平滑切换
                viewPager.setCurrentItem(tab.getPosition(), false);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }

}
